package com.crm.action;

import com.crm.dao.ExamDao;
import com.crm.dao.QuestionDao;
import com.crm.model.PageBean;
import com.crm.model.business.Exam;
import com.crm.model.business.Question;
import com.crm.util.PageUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author: GaoXu
 * @date: 2020/7/23 18:11
 * @Description: (用一句话描述该文件做什么)
 */
public class ExamAction extends ActionSupport implements ServletRequestAware {

    private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private final ExamDao examDao = new ExamDao();
    private final QuestionDao questionDao = new QuestionDao();
    private List<Exam> examList;
    private Exam exam;
    private String mainPage;
    private String page;
    private int total;
    private String pageCode;



    public String save(){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();

        /**
         * 计算总分
         */
        int totalScore = 0, singleScore = 0, moreScore = 0;
        while (iterator.hasNext()) {
            Map.Entry<String, String[]> entry = iterator.next();
            String key = entry.getKey();
            String[] values = entry.getValue();
            String questionId = "";
            String answer = "";
            if ("single".equals(key.split("-")[0])) {
                questionId = key.split("-")[1];
                answer = values[0];
                singleScore += this.calScore(questionId, answer, "1");
            }
            if ("more".equals(key.split("-")[0])) {
                questionId = key.split("-")[1];
                for (String s : values) {
                    answer += s + ",";
                }
                answer = answer.substring(0, answer.length() - 1);
                moreScore += this.calScore(questionId, answer, "2");
            }
        }
        totalScore = singleScore + moreScore;

        exam.setSingleScore(singleScore);
        exam.setMoreScore(moreScore);
        exam.setScore(totalScore);
        exam.setExamDate(new Date());
        examDao.save(exam);
        mainPage = "business/exam/examResult.jsp";
        return SUCCESS;
    }


    public String list() {
        if (StringUtils.isEmpty(page)) {
            page = "1";
        }
        int pageSize = 5;
        PageBean pageBean = new PageBean(Integer.parseInt(page), pageSize);
        if(ObjectUtils.allNotNull(exam)){
            total = examDao.count(exam);
            examList = examDao.list(exam, pageBean);
        }else{
            total = examDao.count();
            examList = examDao.list(pageBean);
        }
        pageCode = PageUtil.genPagination(request.getContextPath() + "/examAction!list", total, Integer.parseInt(page), pageSize);
        mainPage = "business/exam/examList.jsp";
        return SUCCESS;
    }





    private int calScore(String questionId, String userAnswer, String type) {
        Question question = questionDao.getQuestionById(questionId);
        if (userAnswer.equals(question.getAnswer())) {
            if ("1".equals(type)) {
                return 10;
            } else {
                return 20;
            }
        } else {
            return 0;
        }
    }



    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    public String getMainPage() {
        return mainPage;
    }

    public void setMainPage(String mainPage) {
        this.mainPage = mainPage;
    }

    public Exam getExam() {
        return exam;
    }

    public void setExam(Exam exam) {
        this.exam = exam;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getPageCode() {
        return pageCode;
    }

    public void setPageCode(String pageCode) {
        this.pageCode = pageCode;
    }

    public List<Exam> getExamList() {
        return examList;
    }

    public void setExamList(List<Exam> examList) {
        this.examList = examList;
    }
}
